<?php 

class Exam_Api_Core extends Core_Api_Abstract{
    // GET EXAM VIEW PAGE LINK
	public function getHref($exam_id, $owner_id, $slug =null) {

		$params = array_merge(array('user_id' => $owner_id, 'exam_id' => $exam_id, 'slug' => $slug));
		return Zend_Controller_Front::getInstance()->getRouter()
		->assemble($params, 'exam_specific', true);
        }
    public function initTinyMCE()
	{
	  $locale = new Zend_Locale();
	  $lang = $locale->getLanguage();

	  // Initialaze TinyMCE
	  return array(
		'mode'=> "exact",
		'plugins'=> "emotions, xhtmlxtras, preview, paste, style, table, fullscreen, layer",
		'theme'=> "advanced",
		'skin' => 'o2k7',
		'theme_advanced_buttons1'=> "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		'theme_advanced_buttons2'=> "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		'theme_advanced_buttons3'=> 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,|,fullscreen',
		'height'=> "350",
		'language'=> !empty($lang) ? $lang : 'en',
		'directionality'=> "ltr",
		'upload_url'=> Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'upload-photo'), 'exam_general', true),
		'relative_urls'=> "",
		'elements'=> "description,content",
		'width'=> "580",
		'extended_valid_elements'=> "object[width|height|classid|codebase],param[name|value],embed[src|type|width|height|flashvars|wmode]"
		);
	}
    	public function imageTab($baseUrl, $file)
	{
return '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>{#advanced_dlg.image_title}</title>
	<script type="text/javascript" src="'.$baseUrl.'/externals/tinymce/tiny_mce_popup.js"></script>
	<script type="text/javascript" src="'.$baseUrl.'/externals/tinymce/utils/mctabs.js"></script>
	<script type="text/javascript" src="'.$baseUrl.'/externals/tinymce/utils/form_utils.js"></script>
	<script type="text/javascript" src="'.$baseUrl.'/externals/tinymce/themes/advanced/js/image.js"></script>
</head>
<body id="image" style="display: none">
<form onsubmit="ImageDialog.update();return false;" action="#">
	<div class="tabs">
		<ul>
			<li id="general_tab" class="current"><span><a href="'.$baseUrl.'/externals/tinymce/themes/advanced/image.htm" onmousedown="return false;">{#advanced_dlg.image_title}</a></span></li>
			<li id="upload_tab"><span><a href="'.$baseUrl.'/externals/tinymce/themes/advanced/upload.htm" onmousedown="return false;">Upload</a></span></li>
		</ul>
	</div>

	<div class="panel_wrapper">
		<div id="general_panel" class="panel current">
     <table border="0" cellpadding="4" cellspacing="0">
          <tr>
            <td class="nowrap"><label for="src">{#advanced_dlg.image_src}</label></td>
            <td><table border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><input id="src" name="src" type="text" class="mceFocus" value="'.$baseUrl.'/'.$file.'" style="width:200px" onchange="ImageDialog.getImageData();" /></td>
                  <td id="srcbrowsercontainer">&nbsp;</td>
                </tr>
              </table></td>
          </tr>
		  <tr>
			<td><label for="image_list">{#advanced_dlg.image_list}</label></td>
			<td><select id="image_list" name="image_list" onchange="document.getElementById(src).value=this.options[this.selectedIndex].value;document.getElementById(alt).value=this.options[this.selectedIndex].text;"></select></td>
		  </tr>
          <tr>
            <td class="nowrap"><label for="alt">{#advanced_dlg.image_alt}</label></td>
            <td><input id="alt" name="alt" type="text" style="width: 200px" /></td>
          </tr>
          <tr>
            <td class="nowrap"><label for="align">{#advanced_dlg.image_align}</label></td>
            <td><select id="align" name="align" onchange="ImageDialog.updateStyle();">
                <option value="">{#not_set}</option>
                <option value="baseline">{#advanced_dlg.image_align_baseline}</option>
                <option value="top">{#advanced_dlg.image_align_top}</option>
                <option value="middle">{#advanced_dlg.image_align_middle}</option>
                <option value="bottom">{#advanced_dlg.image_align_bottom}</option>
                <option value="text-top">{#advanced_dlg.image_align_texttop}</option>
                <option value="text-bottom">{#advanced_dlg.image_align_textbottom}</option>
                <option value="left">{#advanced_dlg.image_align_left}</option>
                <option value="right">{#advanced_dlg.image_align_right}</option>
              </select></td>
          </tr>
          <tr>
            <td class="nowrap"><label for="width">{#advanced_dlg.image_dimensions}</label></td>
            <td><input id="width" name="width" type="text" value="" size="3" maxlength="5" />
              x
              <input id="height" name="height" type="text" value="" size="3" maxlength="5" /></td>
          </tr>
          <tr>
            <td class="nowrap"><label for="border">{#advanced_dlg.image_border}</label></td>
            <td><input id="border" name="border" type="text" value="" size="3" maxlength="3" onchange="ImageDialog.updateStyle();" /></td>
          </tr>
          <tr>
            <td class="nowrap"><label for="vspace">{#advanced_dlg.image_vspace}</label></td>
            <td><input id="vspace" name="vspace" type="text" value="" size="3" maxlength="3" onchange="ImageDialog.updateStyle();" /></td>
          </tr>
          <tr>
            <td class="nowrap"><label for="hspace">{#advanced_dlg.image_hspace}</label></td>
            <td><input id="hspace" name="hspace" type="text" value="" size="3" maxlength="3" onchange="ImageDialog.updateStyle();" /></td>
          </tr>
        </table>
		</div>
	</div>

	<div class="mceActionPanel">
		<input type="submit" id="insert" name="insert" value="{#insert}" />
		<input type="button" id="cancel" name="cancel" value="{#cancel}" onclick="tinyMCEPopup.close();" />
	</div>
</form>
</body>
</html>';
  }
  public function createPhoto($img, $user_id, $parent_id, $type = 'exam', $parent_type = 'exam')
  {
    $imageObject = array();
    if( $img instanceof Zend_Form_Element_File ) {
      $file = $img->getFileName();
      $fileName = basename($file);
    }
	else if( is_array($img) && !empty($img['tmp_name']) ) {
      $file = $img['tmp_name'];
      $fileName = $img['name'];
    }
	else return false;

    if( !$fileName ) {
      $fileName = $file;
    }
    //storage system
   $params = array(
      'type' => $type,
      'name' => $fileName,
      'parent_type' => $parent_type,
      'parent_id' => $parent_id,
      'user_id' => $user_id,
      'extension' => substr($fileName, strrpos($fileName, '.') + 1),
    );

    $extension = strtolower(ltrim(strrchr($fileName, '.'), '.'));
    $base = rtrim(substr($fileName, 0, strrpos($fileName, '.')), '.');
    $temporary = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';

	$img_type = array('_m', '_p', '_n', '_i');
	$img_param = array(array(720, 720), array(200, 400), array(140, 160), array(48, 48));
	$img_child = array('thumb.profile', 'thumb.normal', 'thumb.icon');
	$create_img = Engine_Api::_()->storage();

	foreach ($img_type as $key => $type) {
	  $path = $temporary . DIRECTORY_SEPARATOR . $base . $type . '.' . $extension;
      $image_tmp = Engine_Image::factory();
      $image_tmp->open($file);
	  if ($type == '_i') { // if icon
   	    $icon_size = min($image_tmp->height, $image_tmp->width);
    	$dst_x = ($image_tmp->width - $icon_size)*0.5;
    	$dst_y = ($image_tmp->height - $icon_size)*0.5;
	    $image_tmp->resample($dst_x, $dst_y, $icon_size, $icon_size, $img_param[$key][0], $img_param[$key][1]);
	  }
	  else $image_tmp->resize($img_param[$key][0], $img_param[$key][1]);
      $image_tmp->write($path);
      $image_tmp->destroy();

	  $imageObject[] = $create_img->create($path, $params);
	  @unlink($path);
	}

	if ($imageObject)
	  foreach($img_child as $key => $child)
	    $imageObject[0]->bridge($imageObject[$key+1], $child);

	return $imageObject[0];	//main photo
  }
}
?>